#!/usr/bin/python3
#-*- coding:utf-8 -*-
'''cpu负载检测'''
import sys,os

#方法一
def check_load1():
    loadf=open('/proc/loadavg','r')
    allavg=loadf.readline()
    load5avg=float(allavg.split()[0])
    load10avg=float(allavg.split()[1])
    load15avg=float(allavg.split()[2])
    loadf.close()
    print(load5avg)
    n=0
    cpunf=open('/proc/cpuinfo','r')
    for i in cpunf.readlines():
        if i.split(':')[0].strip() == 'processor':
            n+=1
    cpunf.close()
    if load5avg > n:
        print("critical - load average:%.2f,%.2f,%.2f" %(load5avg,load10avg,load15avg))
    else:
        print( "OK - load average:%.2f,%.2f,%.2f" %(load5avg,load10avg,load15avg))



def check_load2():
    (d1,d2,d3)=os.getloadavg()
    print(d1)
    n=0
    cpunf=open('/proc/cpuinfo','r')
    for i in cpunf.readlines():
        if i.split(':')[0].strip() == 'processor':
            n+=1
    cpunf.close()
    if d1 > n:
        print("critical - load average:%.2f,%.2f,%.2f" %(d1,d2,d3))
    else:
        print("OK - load average:%.2f,%.2f,%.2f" %(d1,d2,d3))



if __name__ == '__main__':
    check_load1()
    check_load2()